package com.bear.newsset.mvp.model;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bear.newsset.api.QueryNewsListApi;
import com.bear.newsset.api.QuerySiteListApi;
import com.bear.newsset.entity.NewsLableEntity;
import com.bear.newsset.entity.NewsParamEntity;
import com.bear.newsset.entity.ResultSubItemEntity;
import com.bear.newsset.entity.SiteListEntity;
import com.bear.newsset.util.DOM;
import com.bear.newsset.util.HttpUtil;
import com.bear.newsset.util.LogUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.bear.newsset.util.Constant.DOM_ID_CALL_QUERY_NEWS_LABLE_LIST;

/**
 * 新闻请求类
 */
public class NewsRequest {
    /**
     * 回调新闻数据
     */
    public interface CallbackNewsData { void result(@Nullable ResultSubItemEntity data); }

    /**
     * 回调新闻来源列表
     */
    public interface CallbackSiteList { void result(@NonNull SiteListEntity siteListEntity); }
    /**
     * 回调接口新闻类别列表
     */
//    public interface CallbackNewsLableList { void result(Map<String, String[]> map); }
    public interface CallbackNewsLableList {
        void result(Map<NewsLableEntity, List<NewsLableEntity>> map);
    }

    //获取新闻api地址
    private static final QueryNewsListApi NEWS_LIST_API = new QueryNewsListApi();
    private static final QuerySiteListApi SITE_LIST_API = new QuerySiteListApi();

    /**
     * 获取新闻列表
     * @param entity                新闻参数
     * @param mCallbackNewsData     返回新闻列表
     */
    public static void queryNewsList(@NonNull NewsParamEntity entity,
                                     @Nullable CallbackNewsData mCallbackNewsData) {
        NEWS_LIST_API.clear();                  //清除之前的数据
        NEWS_LIST_API.addParamAll( entity );    //添加参数
        //发起请求
        HttpUtil.requestResponse(NEWS_LIST_API, ResultSubItemEntity.class, response -> {
            /* 请求到的数据转换成新闻实体类 RequestNewsEntity */
            ResultSubItemEntity itemEntity = null;
            if( response.getStatus() == 200 ) itemEntity = response.getData();
            //api并发上限容错处理
            if( itemEntity != null && itemEntity.getData() == null ) {
                itemEntity = null;
                LogUtils.e("Error", "getData() of getNewsDataList is null");
            }
            if( mCallbackNewsData != null ) mCallbackNewsData.result( itemEntity );
        });
    }

    /**
     * 查询新闻来源列表
     * @param mCallbackSiteList      返回来源列表
     */
    public static void querySiteList(@Nullable CallbackSiteList mCallbackSiteList) {
        //发起请求、空参请求
        HttpUtil.requestResponse(SITE_LIST_API, SiteListEntity.class, response -> {
            SiteListEntity e = response.getStatus() == 200 ?
                                                    response.getData() : new SiteListEntity();
            if( mCallbackSiteList != null ) mCallbackSiteList.result( e );
        });
    }

    /**
     * 获取新闻类别列表
     * @param mCallbackNewsLableList     回调接口
     */
    public static void queryNewsLableList(@Nullable CallbackNewsLableList mCallbackNewsLableList) {
//        Map<String, String[]> map = new LinkedHashMap<>();
        Map<NewsLableEntity, List<NewsLableEntity>> map2 = new LinkedHashMap<>();
        String[] mainLables = { "综合", "科技", "娱乐", "体育", "军事",
                "社会", "国际", "财经", "汽车", "时尚", "动漫", "游戏" };
        Long[] mainLableIds = { 1000L, 1001L, 1002L, 1003L, 1004L,
                1005L, 1006L, 1007L, 1008L, 1009L, 1010L, 1011L };
        String[][] childLables = {
                { },
                { "数码", "手机", "虚拟现实", "大数据", "电动汽车", "能源", "软件", "电脑", "通信", "手游" },
                {"综艺", "电影" },
                {"NBA", "足球" },
                { },
                { },
                { },
                { },
                { },
                { },
                { },
                { }
        };
        Long[][] childLableIds = {
                { },
                { 11000L, 11001L, 11002L, 11003L, 11004L, 11005L, 11006L, 11007L, 11008L, 11009L },
                { 12000L, 12001L },
                { 13000L, 13001L },
                { },
                { },
                { },
                { },
                { },
                { },
                { },
                { }
        };
        for (int i = 0; i < mainLables.length; i++) {
            List<NewsLableEntity> childList = new ArrayList<>();
            NewsLableEntity mainEntity = new NewsLableEntity();
            mainEntity.setLableId( mainLableIds[i] );
            mainEntity.setLable( mainLables[i] );

            for (int j = 0; j < childLables[i].length; j++) {
                NewsLableEntity childEntity = new NewsLableEntity();
                childEntity.setLableId( childLableIds[i][j] );
                childEntity.setLable( childLables[i][j] );
                childList.add( childEntity );
            }
            map2.put(mainEntity, childList);
//            map.put(mainLables[i], childLables[i]);
        }
//        mCallbackNewsLableList.result( map );
        if( mCallbackNewsLableList != null ) mCallbackNewsLableList.result( map2 );
        DOM.getInstance().setResult(DOM_ID_CALL_QUERY_NEWS_LABLE_LIST, map2);
    }
}
